<?php

class Application_Model_DbTable_Admin extends Zend_Db_Table_Abstract {

    protected $_name = 'Admin';
    public function getAll($where=null,$order='admin_id',$method,$page,$number){
        $start = ($page-1)*$number;
        $end = $start+$number;
        $db=$this->getAdapter();
        if($where != null){
            $where='where '.$where;
        }
        $query = "select a.* from (select RowNumber = ROW_NUMBER() 
                  OVER (ORDER BY c.$order $method),c.* FROM admin c $where) a
                  where a.RowNumber > $start and a.RowNumber <= $end ";
        
        $stmt = $db->query($query);
        return $stmt->fetchAll();

    }
    public function getAdmin($id) {
        $id = (int) $id;
        $row = $this->fetchRow('admin_id = ' . $id);
        if (!$row) {
            throw new Exception("Could not find row $id");
        }
        return $row->toArray();
    }
    public function getTotal($roleid){
        $sql="select count(*) as total from admin where deleted = 0 and role_id=".$roleid;
        $db=$this->getAdapter();
        $stmt = $db-> query($sql);
        $row = $stmt->fetchAll();
        $stmt->closeCursor();

        return $row[0]['total'];
    }
    public function countRows($where=null){
        if($where != null){
            $where='where '.$where;
        }
        $sql="select count(*) as total from admin c $where";
        $db=$this->getAdapter();
        $stmt = $db-> query($sql);
        $row = $stmt->fetchAll();
        $stmt->closeCursor();

        return $row[0]['total'];

    }
        public function getAdminId($username) {
        $row = $this->fetchRow("username = '$username'");
        if (!$row) {
            return null;
        }
        return $row->toArray();
    }
    public function getAdminByName($name){
        $db=$this->getAdapter();
        $stmt = $db-> query("SELECT * FROM admin where lower(name) like
                            '%$name%' and role_id <> 1
                            ");
        $row = $stmt->fetchAll();
        $stmt->closeCursor();
        
        return $row;
    }
    public function addAdmin($name,$username,$email,$phone,$password,$details,$role_id) {
        $data = array(
            'name' => $name,
            'username' => $username,
            'email' => $email,
            'phone' => $phone,
            'password' => $password,
            'details' => $details,
            'role_id' => $role_id
        );
        $this->insert($data);
    }

    public function updateAdminData($id,$name,$email,$phone,$details) {
        $data = array(
            'name' => $name,
            'email' => $email,
            'phone' => $phone,
            'details' => $details
        );
        $this->update($data, 'admin_id = ' . (int) $id);
    }

    public function updateAdminPassword($id,$password) {
        $data = array(
            'password' => sha1($password)
        );
        $this->update($data, 'admin_id = ' . (int) $id);
    }

    public function updatePhoneNumber($id,$phone){
        $data = array(
            'phone' => $phone
        );
        $this->update($data, 'admin_id = ' . (int) $id);

    }

    public function deleteAdmin($id) {
        $data = array("deleted"=>1);
        //$this->delete('admin_id =' . (int) $id);
        $this->update($data, 'admin_id = ' . (int) $id);
    }
    //Function to verify login
    public function checkLogin($username,$password){
      $db=$this->getAdapter();
      $stmt = $db-> query("SELECT admin_id FROM admin where username='$username'
                           and password='".sha1($password)."'");
      $row = $stmt->fetchAll();
      $stmt->closeCursor();
      if(sizeof($row) > 0){
        return true;

      }else{
          return false;
      }
    }
    //function to check whether username already exist or not
    public function checkUser($username){
      $db=$this->getAdapter();
      $stmt = $db-> query("SELECT admin_id FROM admin where username='$username'");
      $row = $stmt->fetchAll();
      $stmt->closeCursor();
      if(sizeof($row) > 0){
        return false;
      }else{
          return true;
      }
    }
        public function getSorting($method,$column){
        $db = $this->getAdapter();
        $sql = "SELECT * FROM admin where role_id <> 1 ORDER BY $column $method";
        $stmt = $db->query($sql);
        return $stmt->fetchAll();
    }
    //Function to verify login

}

